﻿// ***********************************************************************
// Assembly         : OSS.ModelViewer
// Author           : tian_
// Created          : 01-24-2019
//
// Last Modified By : tian_
// Last Modified On : 01-24-2019
// ***********************************************************************
// <copyright file="SlantedImageTestCmd.cs" company="">
//     Copyright ©  2018
// </copyright>
// <summary></summary>
// ***********************************************************************
using System.IO;
using Osg;
using ZFGK.Addins;
using ZFGK.IO;
using ZFGK.OSG.UI;
using ZFGK.OSG.Utility;
using ZFGK.Utility;
using ZFGK.WinForms.Utility;

namespace OSS.ModelViewer.Commands.UserTestCmds
{
    /// <summary>
    /// Class SlantedImageTestCmd.
    /// </summary>
    /// <seealso cref="ZFGK.Addins.IExtendCommand" />
    public class SlantedImageTestCmd : IExtendCommand
    {
        /// <summary>
        /// Executes the specified command data.
        /// </summary>
        /// <param name="cmdData">The command data.</param>
        /// <param name="message">The message.</param>
        /// <returns>CmdResult.</returns>
        public CmdResult Execute(ExtendCmdData cmdData, ref string message)
        {
            var viewForm = cmdData.ViewForm as IViewForm;
            if (viewForm == null)
                return CmdResult.Cancel;
            var osgViewCtrl = viewForm.View as ZfOsgViewCtrl;
            var osgObj = osgViewCtrl.OsgObj;

//             string srcFileName;
//             if (!DialogUtil.Open("选择s3c文件","S3C|*.txt", out srcFileName))
//                 return CmdResult.Cancel;
// 
//             string Folder = srcFileName.ParentPath();
            string folder;
            if (!DialogUtil.OpenDir("选择切斜影像父文件夹", null, out folder))
                 return CmdResult.Cancel;

            var group = new Group();
            group.getOrCreateStateSet().SetLighting(false);
            foreach (var directory in DirectoryUtil.GetDirectories(folder))
            {
                try
                {
                    string filePath = PathUtil.Combine(directory, Path.GetFileName(directory) + ".osgb");
                    if(!File.Exists(filePath))
                        continue;

                    //string hashCode = FileUtil.GetHashCode(filePath);
                    var node = OsgDB._.readNodeFile(filePath);
                    //var node = ZfModelCache.GetNode(filePath, hashCode);
                    if (!node.IsValid)
                    {
                        ZfMessageUtil.ShowError("模型读取失败!");
                        return CmdResult.Cancel;
                    }

                    group.addChild(node);
                }
                catch 
                {
                    
                }
            }

            osgObj.Viewer.getDatabasePager().registerPagedLODs(group);
            osgObj.AddOrReplaceModel("Models", group, folder);
            osgObj.SetView(ViewMode.ShowAll);

            return CmdResult.Succeed;
        }
    }
}
